<?php
/**
 * Framework_Util_TableBuilderCollectionWrapper
 *
 * 
 *
 * @author Application Support Group
 * @copyright University of Georgia
 * @package BAMF
*/

class Framework_Util_TableBuilderCollectionWrapper {



/**
 * Framework_Util_TableBuilderCollectionWrapper::create()
 *
 * Return a collection using information from searchBar.tpl and/or paging data. 
 *
 * @param		string $collectionNodeType The node type for which we are building the collection.
 * @param		string $actionClass The action class that should be associated with the parameter and pager session data.
 * @param		array $baseWhere an array with two values, 'where' which points to a where clause, and 'values' which points to an array of values to substitute for the ?s in the where clause 
 * @param		boolean $keywordSearch Set to true to perform a keyword search.
 * @param		boolean $useSession Set to false to ignore any collection parameters that may be stored in a session for the specified action class.
 *
 * @return		Framework_DB_DaoCollection
 *
 * @package		BAMF
 * 
 */

	public static function create($collectionNodeType, $actionClass = '', $baseWhere = '', $keywordSearch = false, $useSession = true) {
		
		if (strpos($collectionNodeType, APPLICATION_DAO_NODE_PREFIX) === 0) {
			$fullNodeName = $collectionNodeType ;
		} else {
			$fullNodeName = APPLICATION_DAO_NODE_PREFIX . $collectionNodeType ;			
		}
		
		$paramsName = $actionClass . "_params"; 
		$pagerName = $actionClass . "_pager" ;
				
		$nodeSchema = eval( "return Framework_DB_DaoNode::getSchema($fullNodeName::getSchemaFile());" ) ;

		if ( !empty( $_POST[ SBAR_KEY_SUBMIT ]) && $_POST[ SBAR_KEY_SUBMIT ] == SBAR_VAL_SUBMIT ) {
			$collectionParameters = Framework_Util_SearchParams::create($nodeSchema, $baseWhere, $keywordSearch, $fullNodeName) ;
			if ( isset( $_SESSION[ $pagerName ] )) {
				unset( $_SESSION[ $pagerName ] ) ;
			}

		} elseif (!empty($_POST[ SBAR_KEY_RESET ]) && $_POST[ SBAR_KEY_RESET ] == SBAR_VAL_RESET ) {
			if( !empty( $baseWhere )) {
				$collectionParameters = Framework_Util_SearchParams::create($nodeSchema, $baseWhere, $keywordSearch, $fullNodeName) ;
			} else {
			  	$collectionParameters = new Framework_DB_DaoParameters() ;
			}
			if ( isset( $_SESSION[ $pagerName ] )) {
				unset( $_SESSION[ $pagerName ] ) ;
			}
			if ( isset( $_SESSION[ $paramsName ] )) {
				unset( $_SESSION[ $paramsName ] ) ;
			}

		} elseif ( $useSession && isset( $_SESSION[ $paramsName ] )) {
			$collectionParameters = $_SESSION[ $paramsName ] ;
			/*
				For some reason, the fields array we get out of the session is sometimes
				different from the fields array we put into the session. That don't make
				any kind of sense, but we're not actually using the information so let's 
				go ahead and replace whatever we pulled with an empty array.
			*/
			$collectionParameters->fields = array();
		} elseif ( !empty( $baseWhere )) {
			$collectionParameters = Framework_Util_SearchParams::create( $nodeSchema, $baseWhere, $keywordSearch, $fullNodeName ) ;

		} else {
			$collectionParameters = new Framework_DB_DaoParameters() ;
		}
	
		
		
		/*
		 	After collecting the search parameters, we next assemble the sort and
		 	paging data. Other than updating the session key for the appropriate 
		 	collection parameters, you shouldn't need to change anything here.
		 */
		

		// sort variable
		if( !empty( $_GET[ TBUILDER_KEY_SORT ] )) {
			$newSort = (array_key_exists($paramsName, $_SESSION) && 
					get_class($_SESSION[$paramsName]) == 'Framework_DB_DaoParameters' && 
					($_SESSION[$paramsName]->sortvar != $_GET[ TBUILDER_KEY_SORT ]));
			$collectionParameters->sortvar = $_GET[TBUILDER_KEY_SORT];

			
			// If an explicit sort direction has been specified then use
			// that, otherwise just toggle the sort direction
			if (!empty ($_GET[ TBUILDER_KEY_SORTDIR ] )) {
				$collectionParameters->sortdir = $_GET[TBUILDER_KEY_SORTDIR];

			} else {

				if( isset( $_SESSION[ $paramsName ] ) && !Framework_ErrorHandler::isError( $_SESSION[ $paramsName ]->sortdir )) {
					if($newSort) {
						$collectionParameters->sortdir = "ASC";
					} else if( $_SESSION[ $paramsName ]->sortdir == "ASC" ) {
						$collectionParameters->sortdir = "DESC" ;
					} else {
						$collectionParameters->sortdir = "ASC" ;
					}
				}

			}

		} else {
			if( isset( $_SESSION[ $paramsName ] ) && !Framework_ErrorHandler::isError( $_SESSION[ $paramsName ]->sortvar )) {
				$collectionParameters->sortvar = $_SESSION[ $paramsName ]->sortvar ;
				$collectionParameters->sortdir = $_SESSION[ $paramsName ]->sortdir ;
			}
		}

		$collectionPager = null;
		if ( isset($_SESSION[$pagerName]) && !empty($_SESSION[$pagerName]) && !isset( $_GET[ TBUILDER_KEY_SORT ] )) {
			$collectionPager = $_SESSION[$pagerName];
		} else {
			$collectionPager = new Framework_DB_DaoCollectionPager() ;
		}

		// pager current page
		if( !empty( $_GET[ PAGER_KEY_PAGE ] )) {
			if( is_numeric( $_GET[ PAGER_KEY_PAGE ] )) {
				$collectionPager->currpage = $_GET[ PAGER_KEY_PAGE ] ;
			}
		}

		// pager records-per-page
		if( !empty( $_GET[ PAGER_KEY_LIMIT ] )) {
			if( is_numeric( $_GET[ PAGER_KEY_LIMIT ] )) {
				$collectionPager->limit = $_GET[ PAGER_KEY_LIMIT ] ;
				$collectionPager->showmaxrecords = 0 ;
			} elseif( $_GET[ PAGER_KEY_LIMIT ] == 'max' ) {
				$collectionPager = Framework_DB_DaoCollectionPager::getMaxPager() ;
			}
		}
		$collectionPager->calculateOffset() ;
		if($useSession) {
	 		$_SESSION[ $paramsName ] = $collectionParameters ; 
			$_SESSION[ $pagerName ] = $collectionPager ;
		}
		$content = Framework_DB_DaoCollectionFactory::build( $fullNodeName, $collectionParameters, $collectionPager ) ;
		
		return $content ;
	}
}
